﻿// -----------------------------------------------------------------------
// <copyright file="MessageController.cs" company="Support">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SupportSystemDB
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Message Controller Class
    /// </summary>
    public class MessageController : ParentController
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageController"/> class.
        /// </summary>
        /// <param name="supportEntities">The support entities.</param>
        internal MessageController( SupportEntities supportEntities )
            : base( supportEntities )
        {
        }

        /// <summary>
        /// Sends the new message.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="senderId">The sender id.</param>
        /// <param name="messageType">Type of the message.</param>
        /// <param name="receiverIds">The receiver ids.</param>
        public void SendNewMessage( string subject , string body , int senderId , int messageType , List<int> receiverIds )
        {
            Thread temp = new Thread() { TypeId = messageType };
            this.SupportDB.Threads.AddObject( temp );
            this.SupportDB.SaveChanges();
            
            foreach ( int item in receiverIds )
            {
                this.SupportDB.Includes.AddObject( new Include() { ThreadId = temp.Id , MemberId = item , IsArchived = false } );
            }

            this.AddMessage( subject , body , senderId , temp.Id , receiverIds );
            this.SupportDB.SaveChanges();
        }

        /// <summary>
        /// Replies the message.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="senderId">The sender id.</param>
        /// <param name="threadId">The thread id.</param>
        /// <param name="receiverIds">The receiver ids.</param>
        public void ReplyMessage( string subject , string body , int senderId , int threadId , List<int> receiverIds )
        {
            this.AddMessage( subject , body , senderId , threadId , receiverIds );
            this.SupportDB.SaveChanges();
        }

        /// <summary>
        /// Gets the inbox.
        /// </summary>
        /// <param name="memberId">The member id.</param>
        /// <returns>list of member threads</returns>
        public List<vThread> GetInbox( int memberId )
        {
            return this.SupportDB.GetMemberInbox( memberId ).ToList();
        }

        /// <summary>
        /// Gets the messages.
        /// </summary>
        /// <param name="threadId">The thread id.</param>
        /// <returns>List of messages in current thread</returns>
        public List<vMessage> GetMessages( int threadId )
        {
            return this.SupportDB.GetMessagesByThread( threadId ).ToList();
        }

        /// <summary>
        /// Adds the message.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="senderId">The sender id.</param>
        /// <param name="threadId">The thread id.</param>
        /// <param name="receiverIds">The receiver ids.</param>
        private void AddMessage( string subject , string body , int senderId , int threadId , List<int> receiverIds )
        {
            Message temp = new Message()
                {
                    Subject = subject ,
                    Body = body ,
                    SenderId = senderId ,
                    SentDate = DateTime.Now ,
                    ThreadId = threadId
                };
            this.SupportDB.Messages.AddObject( temp );

            this.SupportDB.SaveChanges();

            foreach ( int id in receiverIds )
            {
                this.SupportDB.Receives.AddObject( new Receive { MessageId = temp.Id , MemberId = id , IsOpened = 0 , OpenedDate = DateTime.Now } );
            }
        }
    }
}
